import random
import time
from pprint import pprint
from fake_useragent import UserAgent
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from parse_html import parse_html
from get_cookie import Zlzp


class Driver:
    def __init__(self, name='jucailin03', password='jucailin03', keyword='java'):
        self.keyword = keyword
        options = webdriver.ChromeOptions()
        options.add_argument(
            'Accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8')
        options.add_argument('Accept-Language=zh-CN,zh;q=0.9', )
        options.add_argument('Cache-Control=max-age=0', )
        options.add_argument('Connection=keep-alive', )
        options.add_argument('Host=rd5.zhaopin.com', )
        options.add_argument('Referer=https://rd5.zhaopin.com/custom/searchv2')
        options.add_argument('Upgrade-Insecure-Requests=1')
        options.add_argument('User-Agent=' + UserAgent().random)
        # options.add_argument('--headless')
        self.driver = webdriver.Chrome(chrome_options=options)
        self.driver.maximize_window()
        # self.cookies = Zlzp(name,password).get_cookies()
        self.cookies = [
            {'domain': '.zhaopin.com', 'expiry': 1543972182, 'httpOnly': False, 'name': '__utmt', 'path': '/',
             'secure': False, 'value': '1'},
            {'domain': '.zhaopin.com', 'httpOnly': False, 'name': 'dywec', 'path': '/', 'secure': False,
             'value': '95841923'},
            {'domain': '.zhaopin.com', 'expiry': 1859331581, 'httpOnly': False, 'name': 'sts_deviceid', 'path': '/',
             'secure': False, 'value': '1677be0ae3fb2e-0b33a196594c4-6313363-1049088-1677be0ae403b3'},
            {'domain': '.zhaopin.com', 'httpOnly': False, 'name': 'uiioit', 'path': '/', 'secure': False,
             'value': '3772206659635566556755665c645a72566654635166576751665e6420722066596357665467576652645a725566546357665e670'},
            {'domain': '.zhaopin.com', 'httpOnly': False, 'name': 'sts_sg', 'path': '/', 'secure': False, 'value': '1'},
            {'domain': '.zhaopin.com', 'expiry': 1559739581, 'httpOnly': False, 'name': 'dywez', 'path': '/',
             'secure': False,
             'value': '95841923.1543971582.1.1.dywecsr=(direct)|dyweccn=(direct)|dywecmd=(none)|dywectr=undefined'},
            {'domain': '.zhaopin.com', 'expiry': 1543973397, 'httpOnly': False, 'name': 'sts_sid', 'path': '/',
             'secure': False, 'value': '1677be0ae42d03-0e14298e044103-6313363-1049088-1677be0ae43eaf'},
            {'domain': '.zhaopin.com', 'httpOnly': False, 'name': 'sts_chnlsid', 'path': '/', 'secure': False,
             'value': 'Unknown'},
            {'domain': '.zhaopin.com', 'expiry': 1544025599, 'httpOnly': False, 'name': 'sajssdk_2015_cross_new_user',
             'path': '/', 'secure': False, 'value': '1'},
            {'domain': '.zhaopin.com', 'expiry': 1607043597, 'httpOnly': False, 'name': 'dywea', 'path': '/',
             'secure': False, 'value': '95841923.2811771422873299500.1543971582.1543971582.1543971582.1'},
            {'domain': '.zhaopin.com', 'expiry': 1607043597, 'httpOnly': False, 'name': '__utma', 'path': '/',
             'secure': False,
             'value': '269921210.1083905901.1543971583.1543971583.1543971583.1'},
            {'domain': '.zhaopin.com', 'httpOnly': False, 'name': '__utmc', 'path': '/', 'secure': False,
             'value': '269921210'},
            {'domain': '.zhaopin.com', 'expiry': 1559739597, 'httpOnly': False, 'name': '__utmz', 'path': '/',
             'secure': False,
             'value': '269921210.1543971583.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)'},
            {'domain': '.zhaopin.com', 'expiry': 1543971716.141927, 'httpOnly': False, 'name': 'JsOrglogin',
             'path': '/',
             'secure': False, 'value': '3059461577'},
            {'domain': '.zhaopin.com', 'httpOnly': False, 'name': 'at', 'path': '/', 'secure': False,
             'value': 'ab69bfe2a8a7409b94ecb1151b099203'},
            {'domain': '.zhaopin.com', 'httpOnly': False, 'name': 'Token', 'path': '/', 'secure': False,
             'value': 'ab69bfe2a8a7409b94ecb1151b099203'},
            {'domain': '.zhaopin.com', 'expiry': 1544057996.945927, 'httpOnly': False, 'name': 'zp-route-meta',
             'path': '/',
             'secure': False, 'value': 'uid=1019820525,orgid=31378103'},
            {'domain': '.zhaopin.com', 'expiry': 1546563596.141989, 'httpOnly': False, 'name': 'rt', 'path': '/',
             'secure': False, 'value': 'f5e6f875d88a4ceb982d2a18ef32187c'},
            {'domain': '.zhaopin.com', 'httpOnly': False, 'name': 'RDsUserInfo', 'path': '/', 'secure': False,
             'value': '3e692f645b6a5864436b586b5d66526956735b695e64546a5c64486b256b2a6657690e731e690864076a00641f6b006b0a665a69577350693864326a5464426b526b26662e69597358695a64556a5e644b6b586b546659695f732a693f645b6a5864416b446b51664a695d7351695b645d6a28643f6b546b5466516931732a6956642c6a2064426b506b5c66586954735e695964536a5864486b3c6b3066576955735d695b645d6a3a643a6b546b546651694'},
            {'domain': '.zhaopin.com', 'expiry': 1544835596.945962, 'httpOnly': False, 'name': 'login_point',
             'path': '/',
             'secure': False, 'value': '31378103'},
            {'domain': '.zhaopin.com', 'expiry': 7851171597, 'httpOnly': False, 'name': 'sensorsdata2015jssdkcross',
             'path': '/', 'secure': False,
             'value': '%7B%22distinct_id%22%3A%221019820525%22%2C%22%24device_id%22%3A%221677be0ae501d8-0bd4725e912794-6313363-1049088-1677be0ae51596%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%2C%22first_id%22%3A%221677be0ae501d8-0bd4725e912794-6313363-1049088-1677be0ae51596%22%7D'},
            {'domain': '.zhaopin.com', 'expiry': 1607043597, 'httpOnly': False, 'name': 'NTKF_T2D_CLIENTID',
             'path': '/',
             'secure': False, 'value': 'guestF3DB9EFA-F3B3-A210-7FD5-7BE0EBFB2E05'},
            {'domain': '.zhaopin.com', 'httpOnly': False, 'name': 'nTalk_CACHE_DATA', 'path': '/', 'secure': False,
             'value': '{uid:kf_9051_ISME9754_31378103,tid:1543971597307436}'},
            {'domain': '.zhaopin.com', 'expiry': 1543973397, 'httpOnly': False, 'name': 'sts_evtseq', 'path': '/',
             'secure': False, 'value': '3'},
            {'domain': '.zhaopin.com', 'httpOnly': False, 'name': 'zp_src_url', 'path': '/', 'secure': False,
             'value': 'https%3A%2F%2Fpassport.zhaopin.com%2Forg%2Flogin'},
            {'domain': '.zhaopin.com', 'expiry': 1543973397, 'httpOnly': False, 'name': 'dyweb', 'path': '/',
             'secure': False,
             'value': '95841923.6.6.1543971590118'},
            {'domain': '.zhaopin.com', 'expiry': 1543973397, 'httpOnly': False, 'name': '__utmb', 'path': '/',
             'secure': False,
             'value': '269921210.6.6.1543971590120'},
            {'domain': '.zhaopin.com', 'expiry': 1552611597, 'httpOnly': False, 'name': 'diagnosis', 'path': '/',
             'secure': False, 'value': '0'}]
        self.driver.get('https://rd5.zhaopin.com/custom/searchv2/result')
        self.driver.delete_all_cookies()
        for i in self.cookies:
            self.driver.add_cookie(i)
        self.driver.get('https://rd5.zhaopin.com/custom/searchv2/result')

    def paging(self):
        self.driver.find_element_by_xpath('//*[@id="root"]/div[1]/div[5]/button[2]/i').click()

    def write_keyword(self):
        self.driver.find_element_by_class_name('k-input__inner').send_keys(self.keyword)
        time.sleep(1)
        self.driver.find_element_by_xpath('//*[@id="search-keyword"]/div[2]/img').click()
        time.sleep(3)
        self.driver.find_element_by_xpath(
            '//div[@class="pull-right"]//button[@class="k-button is-primary is-main talent-search"]/i').click()
        time.sleep(3)

    def click_user(self):
        for i in range(random.randint(150, 200)):
            ActionChains(self.driver).key_down(Keys.DOWN).perform()
        time.sleep(3)
        for i in self.driver.find_elements_by_xpath(
                '//*[@id="root"]/div[1]/div[4]/div[3]/table/tbody/tr/td[2]/div/div/div[2]/div/p'):
            try:
                i.click()
                windows = self.driver.window_handles
                self.driver.switch_to.window(windows[-1])
                time.sleep(random.randint(10, 15))
                pprint(parse_html(self.driver.page_source))
                time.sleep(random.randint(10, 15))
                self.driver.close()
                self.driver.switch_to.window(windows[0])
            except:
                continue
        self.paging()


if __name__ == '__main__':
    ok = Driver()
    ok.write_keyword()
    for i in range(5):
        ok.click_user()
    ok.driver.quit()
